package putao.blocksgame.viewobject
{
	import gs.TweenLite;
	import gs.easing.Linear;
	
	import putao.blocksgame.BlocksGameEngine;

	public class MoveItem extends NonmotileItem
	{
		protected var tweener:TweenLite;
		/**
		 * 元件从一格移动到另外一格的间隔 
		 */		
		public var moveInterval:Number = 0.2;
		
		/**
		 * 元件是否在移动中 
		 */		
		public var isInMoving:Boolean = false;
		
		public function MoveItem()
		{
			super();
		}
		
		/**
		 * 
		 * 将元件移动一格，如果在移动中，则不执行
		 * 
		 * @param px -1时左移一格，1时右移一格
		 * @param py -1时上移一格，1时下移一格
		 * 
		 */		
		public function moveOneStep(px:int, py:int):void
		{
			if(isInMoving){
				return;
			}
			var obj:Object = new Object();
			if(px == -1 || px == 1){
				obj.x = this.x + px * BlocksGameEngine.getInstance().getBlockSize();
				isInMoving = true;
			}
			if(py == -1 || py == 1){
				obj.y = this.y + py * BlocksGameEngine.getInstance().getBlockSize();
				isInMoving = true;
			}
			if(isInMoving){
				obj.onComplete = moveStepComplete;
				obj.ease = Linear.easeIn;
				tweener = new TweenLite(this, moveInterval, obj);
			}
		}
		
		private function moveStepComplete():void
		{
			isInMoving = false;
			dispatchEvent(new BlockViewEvent(BlockViewEvent.STEP_MOVE_COMPLETE));
		}
	}
}